package sync;

/*
死锁演示
 */
public class ThreadDemo9 {

  public static void main(String[] args) {

//    创建两个对象
    Object a = new Object();
    Object b = new Object();

    new Thread(()->{

      synchronized (a){
        System.out.println(Thread.currentThread().getName() + "持有a并试图持有b");

        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }

        synchronized (b){
          System.out.println(Thread.currentThread().getName() + "持有b");
        }
      }

    },"A").start();

    new Thread(()->{

      synchronized (b){
        System.out.println(Thread.currentThread().getName() + "持有b并试图持有a");

        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }

        synchronized (a){
          System.out.println(Thread.currentThread().getName() + "持有a");
        }
      }

    },"B").start();

  }
}
